//
//  RoleBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/7.
//

import Foundation
import LeanCloud
//角色
class RoleBean : Codable, Identifiable {
    
    var roleId: String? = nil
    var name: String? = nil//让AI读取的名字
    var name2: String? = nil//对方的称号
    var avatar: String? = nil
    var describe: String? = nil
    var describe2: String? = nil
    var hideDescribe: String? = nil
    var chatDescribe: String? = nil
    var type: String? = nil
    var sexName: String? = nil
    var voiceId: String? = nil
    var bubbleId: String? = nil //气泡
    var bubbleCommentId: String? = nil //评论气泡
    //没有该字段
    var showName: String? = nil
    
    
    func isBot() -> Bool {
        return type == "BOT"
    }
    
    func isUser() -> Bool {
        return type == "USER"
    }
    
    //ai转换
    static func get(_ bean: AiRoleBean) -> RoleBean {
        let role = RoleBean()
        role.roleId  = bean.objectId
        role.name  = bean.name ?? ""
        role.name2  = AiChatExt.getName2(bean)
        role.avatar  = bean.avatar ?? ""
        role.describe  = bean.describe ?? ""
        role.describe2  = bean.describe2 ?? ""
        role.hideDescribe  = bean.hideDescribe ?? ""
        role.chatDescribe = bean.describeChat ?? ""
        role.type  = "BOT"
        role.sexName  = bean.sexName
        role.voiceId  = bean.voice?.voiceId ?? ""
        role.showName = bean.name
        role.bubbleId = bean.bubbleId
        role.bubbleCommentId = bean.bubbleCommentId
        return role
    }
    
    static func get(_ bean: UserBean?, _ botBean: AiRoleBean?) -> RoleBean {
        if let botBean = botBean {
            let botBean2 = get(botBean)
            return get(bean,botBean2)
        } else {
            let botBean: RoleBean? = nil
            return get(bean, botBean)
        }
        
    }
    
    //用户转换
    static func get(_ bean: UserBean?, _ botBean: RoleBean?) -> RoleBean {
        let role = RoleBean()
        let botName2 = botBean?.name2 ?? "" //ai对你的称呼
        if let bean = bean {
            role.roleId = bean.objectId
            role.name = botName2
            role.name2 = ""
            role.avatar = bean.headUrl ?? ""
            role.describe = ""
            role.describe2 = ""
            role.hideDescribe = ""
            role.chatDescribe = ""
            role.type = "USER"
            role.sexName = userManager.getSexStr()
            role.voiceId = nil
            role.showName = bean.name
            role.bubbleId = bean.extraConfig?.bubbleId
            role.bubbleCommentId = bean.extraConfig?.bubbleCommentId
        } else {
            //没有登录，给默认值
            role.roleId = nil
            role.name = botName2
            role.name2 = ""
            role.avatar = ""
            role.describe = ""
            role.describe2 = ""
            role.hideDescribe = ""
            role.type = "USER"
            role.sexName = nil
            role.voiceId = nil
            role.showName = "时光"
        }
        return role
    }
    
    static func getBotDesc(_ botBean: RoleBean) -> String {
        let ta = botBean.sexName == "男" ? "他" : "她"
        let sexName = botBean.sexName == "男" ? "男" : "女"
        let opposite = botBean.sexName != userManager.getSexStr()
        let isCloseAiDescribe = userManager.getExtraConfig().isCloseAiDescribe
        var otherStr = ""
        if opposite && isCloseAiDescribe ?? false {
            otherStr = getPublicAiDescribe(sexName: sexName)
        }
        
        return "\(ta)的名字是：\(botBean.name ?? "" )，性别：\(sexName)，\(botBean.describe ?? "")。\(otherStr)。\(botBean.hideDescribe ?? "")。"
    }
    
    static func getUserDesc(_ botBean: RoleBean) -> String {
        var name = botBean.name2 ?? ""
        if name.isEmpty {
            name = userManager.getName()
        }
        return "关于我的基本信息以及我们的故事：'我的名字：\(name)，性别：\(userManager.getSexStr())；\(botBean.describe2 ?? "")'。"
    }
    
    static func getChatDesc(_ botBean: RoleBean)-> String {
        //取默认的聊天模式
        if ((botBean.chatDescribe ?? "").isEmpty == false) {
            return botBean.chatDescribe ?? ""
        } else {
            return AiDescribeManager.shared.getConfig().chatEndDescribe ?? ""
        }
    }
    
    static func getChatDescDefault(_ botBean: RoleBean)-> String {
        //取默认的聊天模式
        if ((botBean.chatDescribe ?? "").isEmpty == false) {
            return botBean.chatDescribe ?? ""
        } else {
            return AiDescribeManager.shared.getConfig().chatEndDescribeDefault ?? ""
        }
    }
    
    /**
     * 获取公共人设
     */
    static func getPublicAiDescribe(sexName: String?) -> String {
        if ((sexName ?? "").isEmpty || sexName == "男") {
            return configManager.getConfig().getAiDescribe().maleDescribe ?? ""
        }
        return configManager.getConfig().getAiDescribe().femaleDescribe ?? ""
    }
    
    private enum CodingKeys: String, CodingKey {
        case roleId
        case name
        case name2
        case avatar
        case describe
        case describe2
        case hideDescribe
        case type
        case sexName
        case voiceId
        case bubbleId
        case bubbleCommentId
        //        var showName
    }
}
